<?php


namespace app\api\controller;


use app\api\BaseApi;
use app\model\ModelUser;
use app\model\ModelUserLoginLog;
use app\service\UserService;
use think\response\Json;

/**
 * Class Login
 * @package app\api\controller
 */
class Login extends BaseApi
{
    /**
     *用户登录
     *edit by jack 2021-03-28
     */
    public function login():Json
    {
        $mobile = trim(input('mobile'));
        if (empty($mobile)) {
            return $this->error('手机号为空', '', '100102-01');
        }
        $password = trim(input('pwd'));
        if (empty($password)) {
            return $this->error('密码为空', '', '100102-02');
        }
        $password = md5($password);
        $equipment_name = trim(input('equipment_name'));
        if (empty($equipment_name)) {
            return $this->error('设备名称为空', '', '100102-03');
        }
        $login_ip = trim(input('ip'));
        if (empty($login_ip)) {
            return $this->error('登录IP为空', '', '100102-04');
        }
        $userService = new UserService();
        $userInfo = $userService->login($mobile, $password, $login_ip,$equipment_name);
        if ($userInfo['status'] == 1) {
            return $this->success($userInfo['msg'], $userInfo['data']);
        } else {
            return $this->error($userInfo['msg'], $userInfo['data'], '100102-05');
        }
    }

    /**
     *退出登录
     * edit by jack 2021-03-28
     */
    public function loginOut(): Json
    {
        $user_token = trim(input('user_token'));
        if (empty($user_token)) {
            return $this->error('非法访问','',$this::ERROR_CODE);
        }
        $equipment_name = trim(input('equipment_name'));
        if (empty($equipment_name)) {
            return $this->error('设备名称为空','','100102-07');
        }
        $login_ip = trim(input('ip'));
        if (empty($login_ip)) {
            return $this->error('登录IP为空','','100102-08');
        }
        $userService = new UserService();
        $userInfo = $userService-> loginOut($user_token, $equipment_name, $login_ip);
        //dump($userInfo);die();
        if ($userInfo['status'] == 1) {
            return $this->success($userInfo['msg'], $userInfo['data']);
        } else {
            return $this->error($userInfo['msg'], $userInfo['data'],'100103');
        }
    }
}